package com.ccf.business.etl.xxljob.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapBuilder;
import com.alibaba.fastjson.JSON;
import com.common.utils.constant.Constants;
import com.ccf.business.etl.xxljob.config.XxlJobConfig;
import com.ccf.business.etl.xxljob.model.XxlJobInfo;
import com.service.http.service.HttpService;
import com.xxl.job.core.biz.model.ReturnT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 封装调用xxl-job admin后台的接口方法
 * @author zhaoj
 * @create 2021/12/2
 */
@Component
public class XxlJobAdminApi {

    @Autowired
    private XxlJobConfig xxlJobConfig;

    @Autowired
    private HttpService httpService;


    /**
     * 添加任务到xxl-job-admin
     */
    public String addJobInfo(XxlJobInfo xxlJobInfo) {
        //转换参数
        Map<String, Object> param = MapBuilder.<String, Object>create()
                .put("jobGroup",xxlJobConfig.getId())
                .put("jobDesc",xxlJobInfo.getJobDesc())
                .put("author",xxlJobInfo.getAuthor())
                .put("alarmEmail",xxlJobInfo.getAlarmEmail())
                .put("scheduleType",xxlJobInfo.getScheduleType())
                .put("scheduleConf",xxlJobInfo.getScheduleConf())
                .put("misfireStrategy",xxlJobInfo.getMisfireStrategy())
                .put("executorRouteStrategy",xxlJobInfo.getExecutorRouteStrategy())
                .put("executorHandler",xxlJobInfo.getExecutorHandler())
                .put("executorParam",xxlJobInfo.getExecutorParam())
                .put("executorBlockStrategy",xxlJobInfo.getExecutorBlockStrategy())
                .put("executorTimeout",xxlJobInfo.getExecutorTimeout())
                .put("executorFailRetryCount",xxlJobInfo.getExecutorFailRetryCount())
                .put("glueType",xxlJobInfo.getGlueType())
                .build();

        //发送请求
        String reuslt = httpService.postWithCookie(xxlJobConfig.getAddJobUrl(), param, xxlJobConfig.getCookieList());
        System.out.println(reuslt);

        //解析对象
        ReturnT resultObj = JSON.parseObject(reuslt, ReturnT.class);
        if(resultObj.getCode() == ReturnT.SUCCESS_CODE) {
            return (String) resultObj.getContent();
        }
        return Constants.EMPTY;
    }

    /**
     * 更新任务到xxl-job-admin
     */
    public boolean updateJobInfo(XxlJobInfo xxlJobInfo) {
        //转换参数
        Map<String, Object> param = BeanUtil.beanToMap(xxlJobInfo);

        //发送请求
        String reuslt = httpService.postWithCookie(xxlJobConfig.getUpdateJobUrl(), param, xxlJobConfig.getCookieList());
        System.out.println(reuslt);

        //解析对象
        ReturnT resultObj = JSON.parseObject(reuslt, ReturnT.class);

        return resultObj.getCode() == ReturnT.SUCCESS_CODE ? true : false;
    }

    /**
     * 删除任务到xxl-job-admin
     */
    public boolean removeJobInfo(String jobId) {
        //转换参数
        Map<String, Object> param = MapBuilder.<String, Object>create().put("id", jobId).build();

        //发送请求
        String reuslt = httpService.postWithCookie(xxlJobConfig.getRemoveJobUrl(), param, xxlJobConfig.getCookieList());
        System.out.println(reuslt);

        //解析对象
        ReturnT resultObj = JSON.parseObject(reuslt, ReturnT.class);

        return resultObj.getCode() == ReturnT.SUCCESS_CODE ? true : false;
    }

    /**
     * 停止任务到xxl-job-admin
     */
    public boolean stopJobInfo(String jobId) {
        //转换参数
        Map<String, Object> param = MapBuilder.<String, Object>create().put("id", jobId).build();

        //发送请求
        String reuslt = httpService.postWithCookie(xxlJobConfig.getStopJobUrl(), param, xxlJobConfig.getCookieList());
        System.out.println(reuslt);

        //解析对象
        ReturnT resultObj = JSON.parseObject(reuslt, ReturnT.class);

        return resultObj.getCode() == ReturnT.SUCCESS_CODE ? true : false;
    }

    /**
     * 启动任务到xxl-job-admin
     */
    public boolean startJobInfo(String jobId) {
        //转换参数
        Map<String, Object> param = MapBuilder.<String, Object>create().put("id", jobId).build();

        //发送请求
        String reuslt = httpService.postWithCookie(xxlJobConfig.getStartJobUrl(), param, xxlJobConfig.getCookieList());
        System.out.println(reuslt);

        //解析对象
        ReturnT resultObj = JSON.parseObject(reuslt, ReturnT.class);

        return resultObj.getCode() == ReturnT.SUCCESS_CODE ? true : false;
    }

    /**
     * 触发任务到xxl-job-admin
     */
    public boolean triggerJobInfo(String jobId, String executorParam) {
        //转换参数
        Map<String, Object> param = MapBuilder.<String, Object>create()
                .put("id", jobId)
                .put("executorParam", executorParam)
                .build();

        //发送请求
        String reuslt = httpService.postWithCookie(xxlJobConfig.getTriggerJobUrl(), param, xxlJobConfig.getCookieList());
        System.out.println(reuslt);

        //解析对象
        ReturnT resultObj = JSON.parseObject(reuslt, ReturnT.class);

        return resultObj.getCode() == ReturnT.SUCCESS_CODE ? true : false;
    }
}
